var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            pageindex: 0,
            pagesize: 10,
            servicetype: 1, //0-未选择 1-任务工单保修 2-投诉建议
            status: 4, //1-全部 0-处理中 1-已完工 4-待受理 5-待派单 10-待接单 50-已归档=已关单
            RoomID: 0,
            isviewdetail: true,
            onlybaoshi: 0,
            isFirstLoad: 1,
            reload: false
        },
        total: {
            WaitCount: 0,
            ProcessCount: 0,//处理中数量
            CompleteCount: 0,//已完工数量
            TotalCount: 0,//全部数量
            OnFileCount: 0,
            ReadCount: 0,//待受理数量
            SendCount: 0,//---待派单数量
            AcceptCount: 0,//---待接单数量
            GuiDangCount: 0,//---已关单数量
        },
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchmonth: false,
        showsearchparam: false,
        operation: {
            canVisit: false,
            canClose: false,
            canSend: false,
            canAccept: false,
            canAfreshSend: false,
            canChuidan: false,
            canRefuse: false,
            canAcceptOrder: false,
            canComplete: false,
            canHandle: false
        },
        showsearch: false,
        showTop: false,
        searchlist: [],
        projectList: [
            { "ID": "4", "Name": "待受理" },
            { "ID": "5", "Name": "待派单" },
            { "ID": "10", "Name": "待接单" },
            { "ID": "0", "Name": "处理中" },
        ],
        servicetypename:'',//工单类型
        servicetypeid:'',//工单类型id
        chooseId:0,//选择的工单id
    },
    methods: {
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            var options = {};
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            if (that.form.reload) {
                if (that.list.length > 0) {
                    options.pageindex = 0;
                    options.pagesize = that.list.length;
                }
                else {
                    that.form.pageindex = 0;
                    options.pagesize = that.form.pagesize;
                    options.pageindex = that.form.pageindex;
                }
                that.form.reload = false;
            }
            options.RoomID = that.form.RoomID;
            options.ServiceType = that.form.servicetype;
            options.ServiceStatus = that.form.status;
            options.action = "getnewservicelist";
            options.onlybaoshi = that.form.onlybaoshi;
            options.isAnalysis = that.form.isviewdetail ? 1 : 0;
            options.isFirstLoad = that.form.isFirstLoad;
            ns.post(options, function (succeed, data, err) {
                that.is_searching = false;
                if (succeed) {
                    if (data.data.ServiceList.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.list = data.data.ServiceList;
                    } else {
                        that.list = that.list.concat(data.data.ServiceList);
                    }
                    if (that.form.isFirstLoad == 1) {
                        that.total.ReadCount = data.data.ReadCount; //待受理
                        that.total.WaitCount = data.data.WaitCount; //待处理
                        that.total.ProcessCount = data.data.ProcessCount; //处理中
                        that.total.CompleteCount = data.data.CompleteCount;
                        that.total.SendCount = data.data.SendCount;
                        that.total.AcceptCount = data.data.AcceptCount;
                        that.total.GuiDangCount = data.data.GuiDangCount;
                        that.total.TotalCount = data.data.TotalCount;
                    }
                    that.current_item_length = that.list.length;
                    that.form.isFirstLoad = 0;
                } else if (err) {
                    //that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        get_operation: function () {
            var that = this;
            var options = {};
            options.action = "getserviceoperation";
            options.servicetype = that.form.servicetype;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.operation = data.operation;
                } else {
                }
            });
        },
        select_status: function (status) {
            var that = this;
            that.showTop = false;
            that.form.status = status;
            that.list = [];
            that.form.isFirstLoad=1;
            that.get_data();
        },
        openGongdan: function (item) {
            var that = this;
            ns.openWin('home_task_con_frm', '任务', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        doOpenServiceProcess: function (type) {
            ns.openWin('analysis_customerservice_process_frm', '工单进度');
        },
        doSelectSearchParam: function (type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //按月
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;
        },
        doHideSearch: function () {
            var that = this;
            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        doClickDay: function (dataValue) {
            var that = this;
            that.form.activeDate = dataValue;
            var startDate = '',
                endDate = '';
            if (dataValue == 1) {
                var currentDate = dateRangeUtil.getCurrentDate();
                startDate = dateRangeUtil.getDateString(currentDate);
                endDate = startDate;
                that.form.activeDateDesc = '今日';
            }
            if (dataValue == 2) {
                var currentMonth = dateRangeUtil.getCurrentMonth();
                startDate = dateRangeUtil.getDateString(currentMonth[0]);
                endDate = dateRangeUtil.getDateString(currentMonth[1]);
                that.form.activeDateDesc = '本月';
            }
            if (dataValue == 3) {
                var currentYear = dateRangeUtil.getCurrentYear();
                startDate = dateRangeUtil.getDateString(currentYear[0]);
                endDate = dateRangeUtil.getDateString(currentYear[1]);
                that.form.activeDateDesc = '本年';
            }
            that.form.startTime = startDate;
            that.form.endTime = endDate;
        },
        do_select_time: function (type) {
            var that = this;
            var currentTime = type == 1 ? that.form.startTime : that.form.endTime;
            api.openPicker({
                type: 'date',
                date: currentTime,
                title: '选择日期'
            }, function (ret, err) {
                if (ret) {
                    var year = ret.year;
                    var month = (ret.month >= 10 ? ret.month : '0' + ret.month);
                    var day = (ret.day >= 10 ? ret.day : '0' + ret.day);
                    currentTime = year + '-' + month + '-' + day;
                    if (type == 1) {
                        that.form.startTime = currentTime;
                    } else {
                        that.form.endTime = currentTime;
                    }
                }
            });
        },
        //判断是否受理
        do_isShouli:function(item){
            var that=this;
            that.chooseId=item.ID;
            if(!item.ServiceTypeIsDeep){
                ns.openWin('neibubaoshi_typelist', '工单类型选择',{
                    placetype:1,//1-微信公众号改工单类型
                    gongdanType:that.form.servicetype
                });
            }else{
                that.do_open_shouli();
            }
        },
        //修改工单类型
        do_editType(){
            var that = this;
            var options={};
            options.ID=that.chooseId;
            options.TypeID=that.servicetypeid;
            options.TypeName=that.servicetypename;
            options.action="editcustomerservice";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    if(that.chooseId!=0){
                    that.do_open_shouli();
                    }
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },


        // 受理
        do_open_shouli: function () {
            var that = this;
            var options = {};
            options.ID =that.chooseId;
            options.action = "customerserviceshouli";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true) {
                    // that.form.isFirstLoad = 1;
                    that.select_status(5);
                    // api.sendEvent({
                    //     name: 'reloadservicelist',
                    // });

                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });

                }
                api.sendEvent({
                    name: 'reloadservicelist',
                });
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        // 派单
        do_open_chooseuser: function (item, type) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = 'getcustomerservicestatus';
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '派单处理', {
                        source: 'transfer',
                        id: item.ID,
                        type: type,
                        ServiceType: that.form.servicetype
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    
                }
            api.sendEvent({
                        name: 'reloadservicelist',
                    });
            });
        },
        //催单
        do_open_cuidan: function (id) {
            var that = this;
            var options = {};
            options.ID = id;
            options.action = "jpushuser";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    toast.custom({
                        title: "催单成功",
                        html: '<i class="icon iconfont icon-yidu"></i>',
                        duration: 2000
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //拒绝
        do_open_nodialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '拒绝接受该工单，工单将流转至派单人。是否确认拒绝？',
                buttons: ['取消', '确认拒绝']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "refuseservice";
                    ns.post(options, function (succeed, data, err) {
                        if (data.status == true) {
                        } else {
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });

                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });
                }
            })
        },
        //关单
        do_open_dialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons: ['取消', '确认关闭']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "serviceclose";
                    ns.post(options, function (succeed, data, err) {
                        if (succeed) {
                            api.sendEvent({
                                name: 'reload_Analysis',
                            });
                            api.sendEvent({
                                name: 'reloadservicelist',
                            });
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });

                }
            })
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptservice";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        that.form.isFirstLoad = 1;
                        that.select_status(0);
                        // that.get_data();
                        // api.sendEvent({
                        //     name: 'reloadservicelist',
                        // });

                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        
                    }
                    api.sendEvent({
                            name: 'reloadservicelist',
                        });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //完工
        do_complete: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('renwugongdancomplete_frm', '完工记录', {
                        source: 'transfer',
                        id: item.ID,
                        type: 2,
                        ServiceType: item.ServiceType
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                api.sendEvent({
                    name: 'reloadservicelist',
                });
            }, {
                toast: true,
                toastmsg: '提交中'
            });

        },
        //处理
        do_chuli: function (item) {
            var that = this;
            ns.openWin('renwugongdancomplete_frm', '处理记录', {
                source: 'transfer',
                id: item.ID,
                type: 1,
                ServiceType: item.ServiceType
            });
        },
        //回访
        do_huifang: function (item) {
            var that = this;
            ns.openWin('renwugongdanreturnvisit', '回访记录', {
                source: 'transfer',
                id: item.ID,
                type: 3,
                ServiceType: item.ServiceType
            });
        },
        open_select: function () {
            var that = this;
            that.showTop = !that.showTop;
        },
        doHideSearch: function () {
            var that = this;
            that.showTop = false;
            that.showsearch = false;
        },
        select_allstatus: function () {
            var that = this;
            that.showsearch = !that.showsearch;
            var ProjectStatusIDs = [];
            for (var i = 0; i < that.projectList.length; i++) {
                ProjectStatusIDs.push(that.projectList[i].ID);
            }
            var options = {};
            options.ProjectStatusIDs = JSON.stringify(ProjectStatusIDs);//状态筛选，支持多选和单选
            options.action = "getnewservicelist";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.searchlist = data.data.ServiceList;

                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });

        },
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.form.servicetype = api.pageParam.servicetype || 1;
    app.form.RoomID = api.pageParam.id || 0;
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;
    api.addEventListener({
        name: 'reloadservicelist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.form.reload = true;
        app.get_data();
    });
    api.addEventListener({
        name: 'customerservicelist_frm'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'is_hidden_right',
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    ns.init_pull();
    app.get_data();
    setTimeout(function () {
        app.get_operation();
    }, 500)
    api.addEventListener({
        name: 'do_open_neibubaoshi'
    }, function (ret, err) {
        ns.openWin('customerservice_history', '我的历史工单', {
            servicetype: app.form.servicetype
        });
    });
    // api.addEventListener({
    //     name: 'do_open_service_jindu'
    // }, function(ret, err) {
    //     app.doOpenServiceProcess();
    // });
    api.addEventListener({
        name: 'do_open_select'
    }, function (ret, err) {
        app.open_select();
    });
    api.addEventListener({
        name: 'dosaveservicetype'
    }, function (ret) {
        app.servicetypename = ret.value.item.text;
        app.servicetypeid = ret.value.item.id;
        app.do_editType();

    })
};
